package com.sist.ocs.receiver;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName FirstConsumer
 * @Description TODO
 * @Author: xmk
 * @Date: 2018/10/25 17:14
 * @Version: 1.0
 */
@Component
public class FirstConsumer {

    @RabbitListener(queues = {"second-queue"})
    public void handleMessage(String msg , Channel channel, Message message) {
        System.out.println("FirstConsumer {} handleMessage :" + msg);
        System.out.println("channel :" + channel);
        System.out.println("message Correlation:" + message.getMessageProperties().getCorrelationId());

        try {
            /**
             * deliveryTag:该消息的index
             * multiple：是否批量.true:将一次性ack所有小于deliveryTag的消息。
             */
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            /**
             * deliveryTag:该消息的index
             * multiple：是否批量.true:将一次性拒绝所有小于deliveryTag的消息。
             * requeue：被拒绝的是否重新入队列
             */
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);

            /**
             * deliveryTag:该消息的index
             * requeue：被拒绝的是否重新入队列
             *
             * channel.basicNack 与 channel.basicReject 的区别在于basicNack可以拒绝多条消息，而basicReject一次只能拒绝一条消息
             */
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            System.out.println("receiver success");
        } catch (IOException e) {
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            e.printStackTrace();
        }
    }
}
